<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <p>
    <button id="run" type="button" onclick="RunMacro();">导入并运行测试用例</button>
    <a href="/demo/ui.vision.html?direct=1&&macro=Embedded_verifyElementPresent" target="_blank">
      打开入口文件
    </a>
    <button id="runOne" type="button" onclick="RunOneMacro();">运行已存在的测试用例</button>
  </p>
  <script>
    // 运行已存在的测试用例
    function RunOneMacro() {
      (function (detail) {
        var isExtensionLoaded = function () {
          var $root = document.documentElement
          return !!$root && !!$root.getAttribute('data-kantu')
        }
        var openExternal = function (url) {
          const $el = document.createElement('a')
          $el.setAttribute('target', '_blank')
          $el.setAttribute('href', url)
          $el.style.position = 'absolute'
          $el.style.top = '-9999px'
          $el.style.left = '-9999px'
          document.body.appendChild($el)
          $el.click()
          setTimeout(() => {
            $el.remove()
          }, 200)
        }
        var openWebsite = function () {
          openExternal('https://ui.vision/')
        }
        if (!isExtensionLoaded()) {
          if (confirm('RPA IDE未安装，是否需要下载安装?')) {
            return openWebsite()
          }
        } else {
          return window.dispatchEvent(new CustomEvent('kantuSaveAndRunMacro', { detail: detail }))
        }
      })
        ({
          invokeSecret: Math.random(), // 判断是否为此次调用的响应
          direct: true, //If the website URL is whitelisted, run the macro without prompt
          closeKantu: false, //keep the <em>Ui.Vision RPA</em> UI open after the macro is completed
          //Macro JSON code here. You can copy and paste the code directly from the <em>Ui.Vision RPA</em> source tab, https://ui.vision/docs#sourcecodetab
          html: true,
          macro: '测试录制',
          noImport: true,
          storageMode: 'browser',
          json: {}
        })
    }
    // 导入并运行测试用例
    function RunMacro() {
      (function (detail) {
        var isExtensionLoaded = function () {
          var $root = document.documentElement
          return !!$root && !!$root.getAttribute('data-kantu')
        }
        var openExternal = function (url) {
          const $el = document.createElement('a')
          $el.setAttribute('target', '_blank')
          $el.setAttribute('href', url)
          $el.style.position = 'absolute'
          $el.style.top = '-9999px'
          $el.style.left = '-9999px'
          document.body.appendChild($el)
          $el.click()
          setTimeout(() => {
            $el.remove()
          }, 200)
        }
        var openWebsite = function () {
          openExternal('https://ui.vision/')
        }
        if (!isExtensionLoaded()) {
          if (confirm('RPA IDE未安装，是否需要下载安装?')) {
            return openWebsite()
          }
        } else {
          return window.dispatchEvent(new CustomEvent('kantuSaveAndRunMacro', { detail: detail }))
        }
      })
        ({
          invokeSecret: Math.random(), // 判断是否为此次调用的响应
          direct: true, //If the website URL is whitelisted, run the macro without prompt
          closeKantu: false, //keep the <em>Ui.Vision RPA</em> UI open after the macro is completed
          //Macro JSON code here. You can copy and paste the code directly from the <em>Ui.Vision RPA</em> source tab, https://ui.vision/docs#sourcecodetab
          json: {
            "Name": "Embedded_verifyElementPresent",
            "CreationDate": "2018-12-18",
            "Commands": [
              {
                "Command": "open",
                "Target": "https://www.baidu.com/",
                "Value": ""
              },
              {
                "Command": "store",
                "Target": "2",
                "Value": "!timeout_wait"
              },
              {
                "Command": "store",
                "Target": "fast",
                "Value": "!replayspeed"
              },
              // {
              //   "Command": "selectWindow",
              //   "Target": "tab=close",
              //   "Value": ""
              // }
            ]
          }
        })
    }

    // 接收content_script发送来的消息
    window.addEventListener("message", function (e) {
      console.log(e.data, 'main_script')
      if (e.data.cmd === 'INVOKE_RESPONSE') {
        // 调用响应 todo
        alert(JSON.stringify(e.data.data))
        if (e.data.invokeSecret === '') {
          // todo : 校验 invokeSecret
        }
      }
    }, false);
  </script>
</body>

</html>